<template>
  <div>
    <el-card>
      <div slot="header" class="clearfix">
        <el-input
          placeholder="角色名称"
          style="width:200px"
          v-model="searchforName"
        ></el-input>
        <el-button
          type="primary"
          style="margin-left: 20px;"
          @click="searchName"
        >
          <i class="el-icon-search"></i> 查询</el-button
        >
        <el-button @click="resetInput">清空</el-button>
      </div>
      <p>
        <el-button type="primary" @click="showRoleDialog()">添加</el-button>
        <el-button type="danger" @click="deletes">批量删除</el-button>
      </p>
      <el-table
        :data="roleList"
        ref="multipleTable"
        tooltip-effect="dark"
        style="width: 100%"
        border
      >
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column label="序号" type="index" width="100" align="center">
        </el-table-column>
        <el-table-column label="角色名称">
          <template v-slot="{ row }">
            <span v-if="!row.edit">{{ row.roleName }}</span>
            <div v-else>
              <el-input
                v-model="row.roleName"
                class="edit-input"
                size="small"
              />
              <el-button
                class="cancel-btn"
                size="small"
                icon="el-icon-refresh"
                type="warning"
                @click="cancelEdit(row)"
              >
                取消
              </el-button>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="300" align="center">
          <template v-slot="{ row }">
            <el-button size="small" type="info" @click="toInfo(row)">
              <i class="el-icon-info"></i>
            </el-button>
            <el-button
              size="small"
              type="primary"
              title="确定"
              @click="updateRole(row)"
              ><i class="el-icon-check"></i
            ></el-button>
            <el-button
              size="small"
              type="primary"
              title="修改角色"
              @click="toEdit(row)"
              ><i class="el-icon-edit-outline"></i
            ></el-button>
            <el-button size="small" type="danger" @click="deleteRole(row.id)"
              ><i class="el-icon-delete"></i
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="sizechange"
        @current-change="currentchange"
        :current-page="current"
        :page-sizes="[5, 10, 20]"
        :page-size="limit"
        layout=" prev, pager, next, jumper, ->,sizes,total"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <el-dialog
      title="添加新角色"
      :visible.sync="isAddDialogVisible"
      width="30%"
      :before-close="handleClose"
    >
      <el-form
        :model="saveForm"
        :rules="rules"
        ref="ruleForm"
        class="demo-ruleForm"
      >
        <el-form-item label="请输入新名称" prop="roleName">
          <el-input v-model="saveForm.roleName"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="resetForm('ruleForm')">取 消</el-button>
        <el-button type="primary" @click="addRole('ruleForm')">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import {
  deleteRoleById,
  getRolePageList,
  removeRole,
  saveRole,
  updateRoleApi
} from "@/api/acl/role";
import { cloneDeep } from "lodash";

export default {
  name: "Role",
  data() {
    return {
      /* 分页列表的相关数据 */
      current: 1,
      limit: 5,
      total: 0,
      roleList: [],

      // 模糊查询的数据
      searchforName: "",

      // 添加角色的数据
      isAddDialogVisible: false,
      saveForm: {
        roleName: ""
      },
      updateForm: {
        roleName: ""
      },
      rules: {
        roleName: [
          { required: true, message: "角色名称是必填项!", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    // 去分配权限
    toInfo(row) {
      this.$router.push({
        name: "Auth",
        params: {
          roleId: row.id,
          roleName: row.roleName
        }
      });
    },
    // 切换为编辑状态
    toEdit(row) {
      this.$set(row, "edit", true);
    },
    /*
    取消修改
    */
    cancelEdit(row) {
      row.edit = false;
      this.$message.warning("取消角色修改");
    },

    /*
    更新角色
    */
    async updateRole(row) {
      this.$set(row, "edit", false);
      await updateRoleApi({ id: row.id, roleName: row.roleName });
      this.$message.success("更新角色成功!");
    },
    // 删除单个
    deleteRole(id) {
      this.$confirm("确定要删除此角色吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(async () => {
          await deleteRoleById(id);
          this.$message({
            type: "success",
            message: "删除成功!"
          });
          this.getList();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    // 批量删除
    async deletes() {
      let arr = [];
      this.$refs.multipleTable.selection.forEach((Ele, index) => {
        arr.push(Ele.id);
        // console.log(Ele.id);
      });
      // console.log(arr);
      // 发送批量删除的请求
      await removeRole(arr);
      // this.deleteIds = [];
      this.getList();
    },
    // 关闭Dialog的事件函数
    handleClose() {
      this.isAddDialogVisible = true;
      this.$message({
        type: "info",
        message: "取消添加"
      });
    },
    // 点击添加的事件函数
    showRoleDialog() {
      this.isAddDialogVisible = true;
    },
    // 取消按钮
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.isAddDialogVisible = false;
    },
    // 确认添加的事件函数
    addRole(formName) {
      this.$refs[formName].validate(async valid => {
        if (valid) {
          await saveRole(this.saveForm);
          this.$message({
            type: "success",
            message: "添加成功"
          });
          this.saveForm.roleName = "";
          this.getList();
          this.isAddDialogVisible = false;
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 清空搜索框
    resetInput() {
      this.searchforName = "";
      this.getList();
    },
    // 模糊查询
    async searchName() {
      const result = await getRolePageList(1, 5, {
        roleName: this.searchforName
      });
      // console.log(result);
      this.roleList = result.data.items;
      this.total = result.data.total;
      this.searchforName = "";
    },
    // 分页器触发的事件函数
    currentchange(val) {
      this.current = val;
      this.getList();
    },
    sizechange(val) {
      this.limit = val;
      this.getList();
    },
    async getList() {
      const result = await getRolePageList(this.current, this.limit);
      // console.log(result);
      this.roleList = result.data.items;
      this.total = result.data.total;
    }
  },
  mounted() {
    this.getList();
  }
};
</script>
<style></style>
